////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "Timer/Unix/Timer.h"

#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>

namespace CasaEngine
{

/*
double Timer::GetSystemTime()
{
    timeval Time = {0, 0};
    gettimeofday(&Time, NULL);

    return Time.tv_sec + Time.tv_usec / 1000000.;
}

double Timer::DiffSystemTime(double start_, double end_)
{
	return end_ - start_;
}
*/

	///////////////////////////////////////////////////////////////////////////////
	// constructor
	///////////////////////////////////////////////////////////////////////////////
	Timer::Timer()
	{
		startCount.tv_sec = startCount.tv_usec = 0;
		endCount.tv_sec = endCount.tv_usec = 0;

		stopped = 0;
		startTimeInMicroSec = 0;
		endTimeInMicroSec = 0;
	}



	///////////////////////////////////////////////////////////////////////////////
	// distructor
	///////////////////////////////////////////////////////////////////////////////
	Timer::~Timer()
	{
	}



	///////////////////////////////////////////////////////////////////////////////
	// start timer.
	// startCount will be set at this point.
	///////////////////////////////////////////////////////////////////////////////
	void Timer::start()
	{
		stopped = 0; // reset stop flag
		gettimeofday(&startCount, NULL);
	}



	///////////////////////////////////////////////////////////////////////////////
	// stop the timer.
	// endCount will be set at this point.
	///////////////////////////////////////////////////////////////////////////////
	void Timer::stop()
	{
		stopped = 1; // set timer stopped flag
		gettimeofday(&endCount, NULL);
	}



	///////////////////////////////////////////////////////////////////////////////
	// compute elapsed time in micro-second resolution.
	// other getElapsedTime will call this first, then convert to correspond resolution.
	///////////////////////////////////////////////////////////////////////////////
	double Timer::getElapsedTimeInMicroSec()
	{
		if(!stopped)
			gettimeofday(&endCount, NULL);

		startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
		endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;

		return endTimeInMicroSec - startTimeInMicroSec;
	}



	///////////////////////////////////////////////////////////////////////////////
	// divide elapsedTimeInMicroSec by 1000
	///////////////////////////////////////////////////////////////////////////////
	double Timer::getElapsedTimeInMilliSec()
	{
		return this->getElapsedTimeInMicroSec() * 0.001;
	}



	///////////////////////////////////////////////////////////////////////////////
	// divide elapsedTimeInMicroSec by 1000000
	///////////////////////////////////////////////////////////////////////////////
	double Timer::getElapsedTimeInSec()
	{
		return this->getElapsedTimeInMicroSec() * 0.000001;
	}



	///////////////////////////////////////////////////////////////////////////////
	// same as getElapsedTimeInSec()
	///////////////////////////////////////////////////////////////////////////////
	double Timer::getElapsedTime()
	{
		return this->getElapsedTimeInSec();
	}

} // namespace CasaEngine
